﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BusinessEntity;
using BusinessRule;
using BusinessRule.Organization;
using learun.util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProjectManApi.Extensions;
using ProjectManApi.mmm;

namespace ProjectManApi.Controllers
{
    public class UsersController : MvcControllerBase
    {

        private readonly IOperator _operator;
        private readonly IHttpContextAccessor _iHttpContextAccessor;
        public UsersController(IOperator ioperator, IHttpContextAccessor iHttpContextAccessor)
        {
            _operator = ioperator;
            _iHttpContextAccessor = iHttpContextAccessor;
        }

        /// <summary>
        /// 登录接口
        /// </summary>
        /// <param name="username">账号</param>
        /// <param name="password">密码</param>
        /// <returns>登录令牌信息</returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> Login([FromForm]string username, [FromForm]string password)
        {
            #region 写入日志
            LogEntity logEntity = new LogEntity();
            logEntity.F_CategoryId = 1;
            logEntity.F_OperateTypeId = ((int)OperationType.Login).ToString();
            logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.ApiLogin);
            logEntity.F_OperateAccount = username;
            logEntity.F_OperateUserId = username;
            logEntity.F_IPAddress = _iHttpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
            logEntity.F_Module = ConfigHelper.GetConfig().SoftName;
            #endregion

            #region 内部账户验证
            AUserEntity userEntity = await UserRule.GetEntityByAccount(username);
            if (userEntity == null)
            {
                logEntity.F_ExecuteResult = 0;
                logEntity.F_ExecuteResultJson = "没有此账号!";
                await LogRule.Write(logEntity);
                return Fail("账号密码不匹配");
            }

            if (userEntity.F_EnabledMark != 1)
            {
                logEntity.F_ExecuteResult = 0;
                logEntity.F_ExecuteResultJson = "账户被系统锁定,请联系管理员!";
                await LogRule.Write(logEntity);
                return Fail("账户被系统锁定,请联系管理员!");
            }
            bool isOk = UserRule.IsPasswordOk(userEntity.F_Password, password, userEntity.F_Secretkey);


            if (!isOk)//登录失败
            {
                logEntity.F_ExecuteResult = 0;
                logEntity.F_ExecuteResultJson = "账号密码不匹配";
                await LogRule.Write(logEntity);
                return Fail("账号密码不匹配");
            }
            else
            {
                logEntity.F_ExecuteResult = 1;
                logEntity.F_ExecuteResultJson = "登录成功";
                await LogRule.Write(logEntity);
                string token = _operator.EncodeToken(userEntity.F_UserId, userEntity.F_RealName, username);
                return Success(token);
            }
            #endregion
        }
    }
}